package com.ethan.java8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author ethan
 * create 2018-08-03 上午10:20
 * desc
 **/
public class Test2 {
    public static void main(String[] args){
        //第一种方法引用是构造器引用，语法Class::new
        final Car car =  Car.create(Car::new);
        car.name="baoma";
        final List<Car> cars = Arrays.asList(car);
        //第二种方法引用是静态方法引用，语法 Class::static_method
        cars.forEach(Car::collide);
        //第三种方法引用是特定类的任意对象的方法引用，语法 Class::method
        cars.forEach(Car::repair);
        //第四种方法引用是特定对象的方法引用，语法 instance::method
        final Car police = Car.create(Car::new);
        cars.forEach(police::follow);

        String a ="";
    }


    static class Car{
        private String name;
        public static Car create(final Supplier<Car> supplier){
            return supplier.get();
        }

        public static void collide(final Car car){
            System.out.println("Collided"+car.toString());
        }

        public void follow(final Car another){
            System.out.println("Following the "+another.toString());
        }

        public void repair(){
            System.out.println("Repaired"+this.toString());
        }
    }

}


